<template>
  <div class="top-menu">
    <el-menu
      :default-active="activeIndex"
      mode="horizontal"
      text-color="#333"
    >
      <template v-for="(item,index) in items">
        <el-menu-item
          :key="index"
          :index="item.parentId+''"
          @click.native="openMenu(item)"
        >
          <template slot="title">
            <i :class="item.icon" />
            <span>{{ generateTitle(item) }}</span>
          </template>
        </el-menu-item>
      </template>
    </el-menu>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  name: 'TopMenu',
  data() {
    return {
      activeIndex: '0',
      items: []
    }
  },
  computed: {
    ...mapGetters(['tagCurrent', 'menu'])
  },
  inject: ['index'],
  created() {
    this.getMenu()
  },
  methods: {
    openMenu(item) {
      this.index.openMenu(item)
    },
    getMenu() {
      this.$store.dispatch('GetTopMenu').then(res => {
        this.items = res
      })
    },
    generateTitle(item) {
      return this.$router.$avueRouter.generateTitle(
        item.label,
        (item.meta || {}).i18n
      )
    }
  }
}
</script>
